package code.jam.core;

import java.io.File;
import java.util.Vector;
import android.util.Log;
import code.jam.context.JamAndroidVoiceContext;
import code.jam.context.JamConfigContext;
import code.jam.context.JamElecCleanGuitarContext;
import code.jam.context.JamElecDistGuitarContext;
import code.jam.context.JamInstrumentContext;
import code.jam.context.JamPianoContext;
import code.jam.context.JamURI;

public class JamInstrumentManager extends java.lang.Object
{
	public static final String			DEBUG_TAG					= "JamInstrumentManager";

	private JamElecCleanGuitarContext	elec_clean_guitar_context	= null;
	private JamPianoContext				piano_context				= null;
	private JamAndroidVoiceContext		android_voice_context		= null;
	private JamInstrumentContext		current_instrument_context	= null;
	private JamElecDistGuitarContext	elec_dist_guitar_context	= null;
	private Vector<String>				piano_names					= null;
	private Vector<String>				classic_guitars				= null;
	private Vector<String>				elec_clean_guitars			= null;
	private Vector<String>				elec_dist_guitar_names		= null;
	
	private String						selected_instrument			= null;
	private StringBuilder 				previous_instrument 		= null;
	
	boolean is_loaded = false;
	boolean is_checked = false;

	private int play_octave = 1;

	protected JamInstrumentManager()
	{
		super();
		
		previous_instrument = new StringBuilder();
		
		is_checked = checkDownloadInstruemnt();
		Log.d(DEBUG_TAG, String.valueOf(is_checked));
		
		if(is_checked == true)
		{
			is_loaded = true;
		}
	}
	
	public boolean isOk()
	{
		return checkDownloadInstruemnt();
	}

	public void start()
	{
		updateInstruments();
		
		is_checked = true;
		is_loaded = true;

		Log.d(DEBUG_TAG, "instrument manager is now ready...");
	}
	
	public boolean isLoaded()
	{
		return is_loaded;
	}
	
	public boolean isChecked()
	{
		return is_checked;
	}
	
	private boolean checkDownloadInstruemnt()
	{
		JamConfigContext jam_config = JamManager.getInstance().getJamConfigContext();
		
		piano_names = jam_config.getPianoNames();
		classic_guitars = jam_config.getAndroidVoice();
		elec_clean_guitars = jam_config.getElecCleanGuitar();
		elec_dist_guitar_names = jam_config.getElecDistGuitarNames();
		
		File[] files = new File(JamURI.JAM_URL_INSTRUMENT_PIANO_PATH).listFiles();
		if ((files == null) || (files.length == 0) || (files.length != piano_names.size()))
		{
			return false;
		}
		
		files = new File(JamURI.JAM_URL_INSTRUMENT_ELEC_DIST_GUITAR).listFiles();
		if ((files == null) || (files.length == 0) || (files.length != elec_dist_guitar_names.size()))
		{
			return false;
		}
		
		files = new File(JamURI.JAM_URL_INSTRUMENT_ELEC_CLEAN_GUITAR_PATH).listFiles();
		if ((files == null) || (files.length == 0) || (files.length != elec_clean_guitars.size()))
		{
			return false;
		}
		
		files = new File(JamURI.JAM_URL_INSTRUMENT_ANDROID_VOICE_PATH).listFiles();
		if ((files == null) || (files.length == 0) || (files.length != classic_guitars.size()))
		{
			return false;
		}
		
		return true;
	}

	private void updateInstruments()
	{
		try
		{
			JamConfigContext jam_config = JamManager.getInstance().getJamConfigContext();

			piano_names = jam_config.getPianoNames();
			classic_guitars = jam_config.getAndroidVoice();
			elec_clean_guitars = jam_config.getElecCleanGuitar();
			elec_dist_guitar_names = jam_config.getElecDistGuitarNames();

			JamDataManager.downloadInstrumentsToSD(piano_names, JamURI.JAM_URL_INSTRUMENT_PIANO_PATH, JamURI.JAM_SD_INSTRUMENT_PIANO_PATH);

			JamDataManager.downloadInstrumentsToSD(classic_guitars, JamURI.JAM_URL_INSTRUMENT_ANDROID_VOICE_PATH,
					JamURI.JAM_SD_INSTRUMENT_ANDROID_VOICE_PATH);

			JamDataManager.downloadInstrumentsToSD(elec_clean_guitars, JamURI.JAM_URL_INSTRUMENT_ELEC_CLEAN_GUITAR_PATH,
					JamURI.JAM_SD_INSTRUMENT_ELEC_CLEAN_GUITAR_PATH);

			JamDataManager.downloadInstrumentsToSD(elec_dist_guitar_names, JamURI.JAM_URL_INSTRUMENT_ELEC_DIST_GUITAR,
					JamURI.JAM_SD_INSTRUMENT_ELEC_DIST_GUITAR_PATH);

		}
		catch(NullPointerException e)
		{
			e.printStackTrace();
		}

		return;
	}

	public void updateInstrumentContext()
	{
		if (selected_instrument.compareTo(JamURI.PIANO) == 0)
		{
			JamPianoContext piano_context = new JamPianoContext(piano_names, JamInstrumentContext.MediaPlayerType.SOUND_POOL);
			addPianoContext(piano_context);
		}
		else if (selected_instrument.compareTo(JamURI.ANDROID_VOICE) == 0)
		{
			JamAndroidVoiceContext android_voice_context = new JamAndroidVoiceContext(classic_guitars, JamInstrumentContext.MediaPlayerType.SOUND_POOL);
			addAndroidVoiceContext(android_voice_context);
		}
		else if (selected_instrument.compareTo(JamURI.ELEC_CLEAN_GUITAR) == 0)
		{
			JamElecCleanGuitarContext elec_clean_guitar = new JamElecCleanGuitarContext(elec_clean_guitars, JamInstrumentContext.MediaPlayerType.SOUND_POOL);
			addElecCleanGuitarContext(elec_clean_guitar);
		}
		else if (selected_instrument.compareTo(JamURI.ELEC_DIST_GUITAR) == 0)
		{
			JamElecDistGuitarContext elec_dist_guitar_context = new JamElecDistGuitarContext(elec_dist_guitar_names, JamInstrumentContext.MediaPlayerType.SOUND_POOL);
			addElecDistGuitarContext(elec_dist_guitar_context);
		}
	}

	public void addElecCleanGuitarContext(JamElecCleanGuitarContext context)
	{
		if (this.elec_clean_guitar_context == null)
		{
			this.elec_clean_guitar_context = context;
		}

		return;
	}

	public void addPianoContext(JamPianoContext context)
	{
		if (this.piano_context == null)
		{
			this.piano_context = context;
		}

		return;
	}

	public void addAndroidVoiceContext(JamAndroidVoiceContext context)
	{
		if (this.android_voice_context == null)
		{
			this.android_voice_context = context;
		}
	}

	public void addElecDistGuitarContext(JamElecDistGuitarContext context)
	{
		if (this.elec_dist_guitar_context == null)
		{
			this.elec_dist_guitar_context = context;
		}
	}

	public JamElecCleanGuitarContext getElecCleanGuitarContext()
	{
		return this.elec_clean_guitar_context;
	}

	public JamPianoContext getPianoContext()
	{
		return this.piano_context;
	}

	public JamAndroidVoiceContext getAndroidVoiceContext()
	{
		return android_voice_context;
	}

	public void drop( )
	{
		String inst = selected_instrument;
		
		if (inst.compareTo(JamURI.PIANO) == 0)
		{
			if(piano_context == null)
				return;
			
			piano_context.drop();
			piano_context = null;
			Log.d(DEBUG_TAG, "DROP the piano instrument");
		}
		else if (inst.compareTo(JamURI.ANDROID_VOICE) == 0)
		{
			if(android_voice_context == null)
				return;
	
			android_voice_context.drop();
			android_voice_context = null;
			Log.d(DEBUG_TAG, "DROP the android voice instrument");
		}
		else if (inst.compareTo(JamURI.ELEC_CLEAN_GUITAR) == 0)
		{
			if(elec_clean_guitar_context == null)
				return;
	
			elec_clean_guitar_context.drop();
			elec_clean_guitar_context = null;
			Log.d(DEBUG_TAG, "DROP the elec clean guitar instrument");
		}
		else if (inst.compareTo(JamURI.ELEC_DIST_GUITAR) == 0)
		{
			if(elec_dist_guitar_context == null)
				return;
	
			elec_dist_guitar_context.drop();
			elec_dist_guitar_context = null;
			Log.d(DEBUG_TAG, "DROP the elec dist guitar instrument");
		}
	}
	
	public void dropAll()
	{
		if(piano_context != null)
		{
			piano_context.drop();
			piano_context = null;
			Log.d(DEBUG_TAG, "DROP the piano instrument");
		}
		if(android_voice_context != null)
		{
			android_voice_context.drop();
			android_voice_context = null;
			Log.d(DEBUG_TAG, "DROP the android voice instrument");
		}
		if(elec_clean_guitar_context != null)
		{
			elec_clean_guitar_context.drop();
			elec_clean_guitar_context = null;
			Log.d(DEBUG_TAG, "DROP the elec clean guitar instrument");
		}
		if(elec_dist_guitar_context != null)
		{
			elec_dist_guitar_context.drop();
			elec_dist_guitar_context = null;
			Log.d(DEBUG_TAG, "DROP the elec dist guitar instrument");
		}
	}
	
	public void setCurrentInstrumentName(String instrument)
	{
		selected_instrument = instrument;

		if (instrument.compareTo(JamURI.PIANO) == 0)
		{
			current_instrument_context = piano_context;
		}
		else if (instrument.compareTo(JamURI.ANDROID_VOICE) == 0)
		{
			current_instrument_context = android_voice_context;
		}
		else if (instrument.compareTo(JamURI.ELEC_CLEAN_GUITAR) == 0)
		{
			current_instrument_context = elec_clean_guitar_context;
		}
		else if (instrument.compareTo(JamURI.ELEC_DIST_GUITAR) == 0)
		{
			current_instrument_context = elec_dist_guitar_context;
		}

		return;
	}

	public String getSelectedInstrumentName()
	{
		return this.selected_instrument;
	}

	public JamInstrumentContext getSelectedInstrumentContext()
	{
		if (this.selected_instrument == JamURI.PIANO)
		{
			return this.piano_context;
		}
		else if (this.selected_instrument == JamURI.ANDROID_VOICE)
		{
			return this.android_voice_context;
		}
		else if (this.selected_instrument == JamURI.ELEC_CLEAN_GUITAR)
		{
			return this.elec_clean_guitar_context;
		}
		else if (this.selected_instrument == JamURI.ELEC_DIST_GUITAR)
		{
			return this.elec_dist_guitar_context;
		}

		return null;
	}

	public int getPlayOctave() 
	{
		return play_octave;
	}
	
	public void setOctave(int play_octave)
	{
		this.play_octave = play_octave;
	}

}
